<template>
  <by-background />

  <view class="padding-bottom-20">
    <by-navigation-bar title="去评价" />

    <view class="padding-lr-20">
      <template v-if="list.length">
        <by-list-card
            v-for="(item,index) in list"
            :key="item.uid + index"
            :item="item.technician"
            :user-comment="item.userComment"
            :date="dateFormat(item.createTime,'YYYY年MM月DD日 hh:mm:ss')"
            type="evaluate"
            @showSafeguard="showSafeguard = true"
            @evaluate="addComment(item,$event)"
            @showMenu="handleShowMenu(item)"
        />
        <by-load-footer :has-more="hasMore" text="加载中..." />
      </template>

      <by-nodata v-else />
    </view>
  </view>

  <by-page-dialogs
    v-model:show-safeguard="showSafeguard"
    v-model:show-evaluate="showEvaluate"
    :comment-info="commentInfo"
    :commentValue="commentValue"
    @reload="reload"
  />

  <by-draw-bottom v-model="showMenu" :list="menuList" @click="handleClick"/>
</template>

<script setup lang="ts">
import { getUserComment, deleteComment, deleteCommentStar, deleteCommentTechnician } from '@/common/api'
import { dateFormat, GetList, toast } from '@/common/util'
import type { AnyObject, ICommentInfo } from '@/common/types'

const showSafeguard = ref(false)
const showEvaluate = ref(false)
const showMenu = ref(false)

/* 添加评价*/
const commentInfo = shallowRef<ICommentInfo>({
  tno: '',
  uid: '',
  paySerialNo: ''
})
const commentValue = ref(0)
function addComment(item:AnyObject, e:number) {
  if (e) {
    commentValue.value = e
  } else {
    commentInfo.value = {
      tno: item.tno,
      uid: item.uid,
      paySerialNo: item.paySerialNo
    }
  }
  showEvaluate.value = true
}
watch(showEvaluate, (e) => {
  if (!e) {
    setTimeout(() => {
      commentValue.value = 0
    }, 300)
  }
})

/* 获取列表*/
const hasMore = ref(true)
const list = shallowRef<AnyObject[]>([])
const getList = new GetList(getUserComment, {}, (res:AnyObject) => res.records)
async function reload() {
  list.value = await getList.reload()
  hasMore.value = getList._has_more
}
async function nextPage() {
  hasMore.value = getList._has_more
  list.value.push(...await getList.nextPage())
  triggerRef(list)
}

reload()
onPullDownRefresh(reload)
onReachBottom(nextPage)

/* 菜单点击*/
let serialNo:string = ''
interface IMenuItem{
  text:string
}
const menuList = shallowRef<IMenuItem[]>([])
const setItem = shallowRef<any>({})
function handleShowMenu(item:any) {
  setItem.value = item
  const { paySerialNo, userComment } = item
  menuList.value = userComment && !userComment?.isDeleteStar ? [
    { text: '删除评价' },
    { text: '删除记录' }
  ] : [
    { text: '删除记录' }
  ]
  showMenu.value = true
  serialNo = paySerialNo
}

async function handleClick(e:AnyObject) {
  if (setItem.value.userComment && !setItem.value.userComment?.isDeleteStar && !e.index) {
    await deleteCommentStar({ serialNo })
  } else {
    await deleteComment({ serialNo })
  }
  showMenu.value = false
  reload()
  toast('操作成功')
}

/* 监听返回*/
const back = computed(() => showSafeguard.value || showEvaluate.value || showMenu.value)
onBackPress(() => {
  if (back.value) {
    close()
    return true
  }
})
function close() {
  showSafeguard.value = showEvaluate.value = showMenu.value = false
}
</script>

<style scoped lang="scss">

</style>
